<?php
include("../config.inc.php");
global $conexion;
//----------------------------------------------------
// The use of the data sent from the Javascript side is enabled.
//----------------------------------------------------
$data        = array();

$db_table    = ($_GET['db_table']);
$q_word      = ($_GET['q_word']);
$page_num    = ($_GET['page_num']);
$per_page    = ($_GET['per_page']);
$field       = ($_GET['field']);
$show_field  = ($_GET['show_field']);
$hide_field  = ($_GET['hide_field']);
$select_field=  split(",",($_GET['select_field']));
$order_field = ($_GET['order_field']);
$primary_key = ($_GET['primary_key']);
$order_by    = ($_GET['order_by']);
$val_ant     = (isset($_GET['val_ant']) ? split(";",$_GET['val_ant']) : "");
$tab_ant     = (isset($_GET['tab_ant']) ? split(";", $_GET['tab_ant']) : "");
$whe_ant     = (isset($_GET['whe_ant']) ? split(";", $_GET['whe_ant']) : "");

$offset      = ($page_num - 1) * $per_page;
$show_field  = ($show_field)
	? explode(',',$show_field)
	: array(false);
$hide_field  = explode(',',$hide_field);


//----------------------------------------------------
// connect data base
//----------------------------------------------------
//$db = sqlite_open('../SQLite2/test.sqlite','0600');


//----------------------------------------------------
// get autocomplete candidate
//----------------------------------------------------
$select = "SELECT ".$db_table.".". $select_field[0] . " as nombre, ".$db_table.".". $select_field[1] . " as " . $select_field[1] ;
$from = "FROM     $db_table ";
$where	= "WHERE    ".$db_table.".$field LIKE '%$q_word%'";

$antiguedad = "";

for ($x=0;$x<count($val_ant);$x++) //Busca cual es el ultimo valor utilizado en el siguiente orden PAIS,COMUNIDAD,PROVINCIA,POBLACION
{
	if($val_ant[$x] != 0 && $val_ant[$x] != "")
	{
		$antiguedad = $x;
	}
}
if (is_numeric($antiguedad))
{
	for ($x = count($val_ant);$x>=$antiguedad;$x--)
	{
		$from .= " ".$tab_ant[$x]." ";
		if ($val_ant[$x] != 0 && ($val_ant[$x]!=""))
		{
		$where .= " AND " .$whe_ant[$x]. " = ". $val_ant[$x];}
	}
}
$query = $select . " " . $from . " " . $where;
//echo($query);
$rows  = mysql_query($query,$conexion);

$data['cnt_page'] = 0;
$attached_cnt = 0;
while ($row = mysql_fetch_array($rows,MYSQL_ASSOC))
{
	$data['cnt_page'] ++;
	foreach($row as $key => $value){

		// for "select_only" option
		if($key == $primary_key){
			$data['primary_key'][] = utf8_encode($value);
		}

		// get the value for autocomplete candidate
		if($key == $field){
			$data['candidate'][] = utf8_encode($value);

		} else {

			// for Sub-info
			if(!in_array($key, $hide_field)){

				// It non-displays it in the exclusion column when not corresponding
				// to the display column though it doesn't correspond.
				// However, it doesn't become non-display when there is "*"
				// in the display column.
				if(
					$show_field[0] !== false
					&& !in_array('*', $show_field)
					&& !in_array($key, $show_field)
				){
					continue;
				}

				$data['attached'][$attached_cnt][] = array(
					0 => $key,
					1 => $value
				);
			}
		}
	}
	$attached_cnt++;
}


//----------------------------------------------------
// get the entire number
//----------------------------------------------------
$query = "
	SELECT   $field
	FROM     $db_table
	WHERE    $field LIKE '%$q_word%'
";
$data['cnt'] = mysql_num_rows(mysql_query($query,$conexion) );

echo json_encode($data);


//----------------------------------------------------
// End
//----------------------------------------------------
// cut the connection with the data base
//sqlite_close($db);

?>
